package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootScinfo;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootScinfoSearchForm;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootScinfoService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SessionAttributes("footScinfoSearchForm")
@RequestMapping("/hy/footScinfo")
public class HyScinfoController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footScinfo";
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);

	@Autowired
	private FootScinfoService footScinfoService;
	
	@Autowired
	private FootPersonService footPersonService;
	
	@Autowired
	private FootUserService footUserService;

	/**
	 * 初始化
	 * @param map 参数
	 * @param footScinfoSearchForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, HttpServletRequest request, @ModelAttribute FootScinfoSearchForm footScinfoSearchForm){
		map.addAttribute("qyList", footPersonService.getall());
		map.addAttribute("userList", footUserService.getAllWithoutAdmin(request));
		FootScinfoSearchForm newFootScinfoSearchForm = new FootScinfoSearchForm();
		if(footScinfoSearchForm != null){
			newFootScinfoSearchForm = footScinfoSearchForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootScinfoSearchForm);
	}

	@RequestMapping(value="/search")
	public String search(Model map, HttpServletRequest request,
						   @ModelAttribute FootScinfoSearchForm footScinfoSearchForm) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		footScinfoSearchForm.setUserId(user.getId());
        footScinfoService.search(ENTITYNAME, map, footScinfoSearchForm);
		return LISTPAGE;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(@RequestParam Long qdId, HttpServletRequest request) {
        FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		FootScinfo fs = footScinfoService.findByUserIdAndQyid(user.getId(), qdId);
		if(fs != null){
			if(null != fs.getId()){
				footScinfoService .delete(fs.getId(), request);
			}
			//判断是否有人收藏,并更改兴趣状态
			FootPerson fp = footPersonService.get(qdId);
			List<FootScinfo> scList = footScinfoService.findByQyid(qdId);
			if(scList != null && !scList.isEmpty()){
                fp.setXqzt(1);
			}else{
                fp.setXqzt(0);
			}
			footPersonService.updateNotNull(fp, request);
			return Constants.OK;
		}else{
			return Constants.ERROR;
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String save(@RequestParam Long qdId, HttpServletRequest request) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		if(user.getId() == null || qdId == null){
			return Constants.ERROR;
		}
		FootScinfo fs = new FootScinfo();
		fs.setQyId(qdId);
		FootPerson fp = footPersonService.get(qdId);
		if(fp != null){
            fp.setXqzt(1);
			footPersonService.updateNotNull(fp, request);
		}
		fs.setUserId(user.getId());
		footScinfoService .save(fs, request);
		//更改兴趣状态
		return Constants.OK;
	}

	@RequestMapping(value = "/isSc", method = RequestMethod.POST)
	@ResponseBody
	public Boolean isSc(@RequestParam Long qyId, HttpServletRequest request){
        FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
	    return footScinfoService.isSc(user.getId(), qyId);
	}
	
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootScinfoSearchForm footScinfoSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footScinfoSearchForm);
		map.addAttribute("footPerson", footPersonService.get(id));
		return VIEWPAGE;
	}	
}
